﻿
// TxMediaAppDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "TxMediaApp.h"
#include "TxMediaAppDlg.h"
#include "afxdialogex.h"
#include <sstream>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CTxMediaAppDlg 对话框



CTxMediaAppDlg::CTxMediaAppDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_TXMEDIAAPP_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTxMediaAppDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CTxMediaAppDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BTN_START_PREVIEW, &CTxMediaAppDlg::OnBnClickedBtnStartPreview)
	ON_BN_CLICKED(IDC_BTN_STOP_PREVIEW, &CTxMediaAppDlg::OnBnClickedBtnStopPreview)
	ON_BN_CLICKED(IDC_BTN_START_SEND_VIDEO, &CTxMediaAppDlg::OnBnClickedBtnStartSendVideo)
	ON_BN_CLICKED(IDC_BTN_STOP_SEND_VIDEO, &CTxMediaAppDlg::OnBnClickedBtnStopSendVideo)
	ON_BN_CLICKED(IDC_BTN_START_SEND_AUDIO, &CTxMediaAppDlg::OnBnClickedBtnStartSendAudio)
	ON_BN_CLICKED(IDC_BTN_STOP_SEND_AUDIO, &CTxMediaAppDlg::OnBnClickedBtnStopSendAudio)
	ON_BN_CLICKED(IDC_BTN_START_PLAYOUT, &CTxMediaAppDlg::OnBnClickedBtnStartPlayout)
	ON_BN_CLICKED(IDC_BTN_STOP_PLAYOUT, &CTxMediaAppDlg::OnBnClickedBtnStopPlayout)
	ON_CBN_SELCHANGE(IDC_CBO_CAMERA, &CTxMediaAppDlg::OnCbnSelchangeCboCamera)
	ON_CBN_SELCHANGE(IDC_CBO_RECORDING, &CTxMediaAppDlg::OnCbnSelchangeCboRecording)
	ON_CBN_SELCHANGE(IDC_CBO_PLAYOUT, &CTxMediaAppDlg::OnCbnSelchangeCboPlayout)
	ON_BN_CLICKED(IDC_CHECK_AGC, &CTxMediaAppDlg::OnBnClickedCheckAgc)
	ON_BN_CLICKED(IDC_CHECK_EC, &CTxMediaAppDlg::OnBnClickedCheckEc)
	ON_BN_CLICKED(IDC_CHECK_NS, &CTxMediaAppDlg::OnBnClickedCheckNs)
	ON_BN_CLICKED(IDC_CHECK_AGC_REMOTE, &CTxMediaAppDlg::OnBnClickedCheckAgcRemote)
	ON_BN_CLICKED(IDC_CHECK_NS_REMOTE, &CTxMediaAppDlg::OnBnClickedCheckNsRemote)
	ON_BN_CLICKED(IDC_CHECK_MUTE, &CTxMediaAppDlg::OnBnClickedCheckMute)
	ON_BN_CLICKED(IDC_BTN_RECORD_OUTGOING_VIDEO, &CTxMediaAppDlg::OnBnClickedBtnRecordOutgoingVideo)
	ON_BN_CLICKED(IDC_BTN_RECORD_INCOMING_VIDEO, &CTxMediaAppDlg::OnBnClickedBtnRecordIncomingVideo)
END_MESSAGE_MAP()


// CTxMediaAppDlg 消息处理程序

BOOL CTxMediaAppDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	::SetProcessDPIAware();
	int width = ::GetSystemMetrics(SM_CXSCREEN);
	int height = ::GetSystemMetrics(SM_CYSCREEN);
	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	CWnd* pWnd = GetDlgItem(IDC_STATIC_LOCAL_VIDEO);
	RECT rect;
	pWnd->GetWindowRect(&rect);
	ScreenToClient(&rect);
	_localVideoRenderWnd.Create(NULL, _T("local video window"), WS_CHILD | WS_VISIBLE, rect, this, 1234);

	pWnd = GetDlgItem(IDC_STATIC_REMOTE_VIDEO2);
	pWnd->GetWindowRect(&rect);
	ScreenToClient(&rect);
	_remoteVideoRenderWnd.Create(NULL, _T("remote video window"), WS_CHILD | WS_VISIBLE, rect, this, 1234);

	_txMedia = ITxMedia::create();
	_txMedia->Init();
	_txMedia->SetVideoRenderCallback(std::bind(&CTxMediaAppDlg::OnVideoFrame, this
			,std::placeholders::_1
			,std::placeholders::_2
			,std::placeholders::_3
			,std::placeholders::_4
			,std::placeholders::_5
			,std::placeholders::_6
			,std::placeholders::_7
	));

	std::vector<std::string> videoDevices;
	_txMedia->GetVideoDevices(videoDevices);
	CComboBox* pBox = (CComboBox*)GetDlgItem(IDC_CBO_CAMERA);
	for (int i = 0; i < videoDevices.size(); i++)
	{
		pBox->InsertString(i, videoDevices[i].c_str());
	}
	if (videoDevices.size() > 0)
	{
		pBox->SetCurSel(0);
	}

	std::vector<std::string> playoutDevices;
	_txMedia->GetPlayoutDevices(playoutDevices);
	pBox = (CComboBox*)GetDlgItem(IDC_CBO_PLAYOUT);
	for (int i = 0; i < playoutDevices.size(); i++)
	{
		pBox->InsertString(i, playoutDevices[i].c_str());
	}

	std::vector<std::string> recordingDevices;
	_txMedia->GetRecordingDevices(recordingDevices);
	pBox = (CComboBox*)GetDlgItem(IDC_CBO_RECORDING);
	for (int i = 0; i < recordingDevices.size(); i++)
	{
		pBox->InsertString(i, recordingDevices[i].c_str());
	}

	SetDlgItemText(IDC_EDIT_LOCAL_VIDEO_PORT, "11111");
	SetDlgItemText(IDC_EDIT_LOCAL_AUDIO_PORT, "1111");
	SetDlgItemText(IDC_EDIT_REMOTE_VIDEO_PORT, "11111");
	SetDlgItemText(IDC_EDIT_REMOTE_AUDIO_PORT, "1111");
	SetDlgItemText(IDC_IPADDRESS1, "192.168.3.117");

	OnBnClickedBtnStartPreview();

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CTxMediaAppDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CTxMediaAppDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CTxMediaAppDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CTxMediaAppDlg::OnDestroy()
{
	CDialogEx::OnDestroy();
	_txMedia->Release();
	// TODO: 在此处添加消息处理程序代码
}

void CTxMediaAppDlg::OnVideoFrame(int channel, unsigned char* buffer, int buffer_size,
	int width, int height,
	uint32_t time_stamp, // RTP timestamp in 90kHz.
	int64_t render_time // Wallclock render time in miliseconds>;
)
{
	if (channel == _localVideoChannel)
	{
		_localVideoRenderWnd.Render(buffer, buffer_size, width, height);
	}
	else if (channel == _remoteVideoChannel)
	{
		_remoteVideoRenderWnd.Render(buffer, buffer_size, width, height);
	}
}

void CTxMediaAppDlg::OnBnClickedBtnStartPreview()
{
	CComboBox* pBox = (CComboBox*)GetDlgItem(IDC_CBO_CAMERA);
	int item = pBox->GetCurSel();
	if (item >= 0)
	{
		CString szCamera;
		pBox->GetLBText(item, szCamera);
		std::string camera = szCamera.GetBuffer();
		_localVideoChannel = _txMedia->StartPreviewLocalVideo(camera);
		GetDlgItem(IDC_BTN_START_PREVIEW)->EnableWindow(FALSE);
		GetDlgItem(IDC_BTN_STOP_PREVIEW)->EnableWindow(TRUE);
	}
}

void CTxMediaAppDlg::OnBnClickedBtnStopPreview()
{
	_txMedia->StopPreviewLocalVideo();
	GetDlgItem(IDC_BTN_START_PREVIEW)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_STOP_PREVIEW)->EnableWindow(FALSE);
}

void CTxMediaAppDlg::OnBnClickedBtnStartSendVideo()
{
	CComboBox* pBox = (CComboBox*)GetDlgItem(IDC_CBO_CAMERA);
	int item = pBox->GetCurSel();
	if (item >= 0)
	{
		CString szCamera;
		pBox->GetLBText(item, szCamera);
		std::string camera = szCamera.GetBuffer();
		int localVideoPort = GetDlgItemInt(IDC_EDIT_LOCAL_VIDEO_PORT);
		int remoteVideoPort = GetDlgItemInt(IDC_EDIT_REMOTE_VIDEO_PORT);
		char ip[MAX_PATH] = { 0 };
		GetDlgItemText(IDC_IPADDRESS1, ip, MAX_PATH);
		std::string strIP = ip;
		_remoteVideoChannel = _txMedia->StartSendLocalVideo(camera, localVideoPort, strIP, remoteVideoPort, 640, 480, 500, 30);
		GetDlgItem(IDC_BTN_START_PREVIEW)->EnableWindow(FALSE);
		GetDlgItem(IDC_BTN_STOP_PREVIEW)->EnableWindow(FALSE);
		GetDlgItem(IDC_BTN_START_SEND_VIDEO)->EnableWindow(FALSE);
		GetDlgItem(IDC_BTN_STOP_SEND_VIDEO)->EnableWindow(TRUE);
		GetDlgItem(IDC_BTN_RECORD_OUTGOING_VIDEO)->EnableWindow(TRUE);
		GetDlgItem(IDC_BTN_RECORD_INCOMING_VIDEO)->EnableWindow(TRUE);
	}
}

void CTxMediaAppDlg::OnBnClickedBtnStopSendVideo()
{
	_txMedia->StopSendLocalVideo();
	_txMedia->StopRecordIncomingVideo(_remoteVideoChannel);
	_txMedia->StopRecordOutgoingVideo(_remoteVideoChannel);

	GetDlgItem(IDC_BTN_START_PREVIEW)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_STOP_PREVIEW)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_START_SEND_VIDEO)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_STOP_SEND_VIDEO)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_RECORD_OUTGOING_VIDEO)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_RECORD_INCOMING_VIDEO)->EnableWindow(FALSE);
}

void CTxMediaAppDlg::OnBnClickedBtnStartSendAudio()
{
	int localAudioPort = GetDlgItemInt(IDC_EDIT_LOCAL_AUDIO_PORT);
	int remoteAudioPort = GetDlgItemInt(IDC_EDIT_REMOTE_AUDIO_PORT);
	char ip[MAX_PATH] = { 0 };
	GetDlgItemText(IDC_IPADDRESS1, ip, MAX_PATH);
	std::string strIP = ip;
	if (_audioChannel < 0)
	{
		_audioChannel = _txMedia->CreateAudioChannel(localAudioPort);
	}
	_txMedia->StartSendAudio(_audioChannel, strIP, remoteAudioPort);
	GetDlgItem(IDC_BTN_START_SEND_AUDIO)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_STOP_SEND_AUDIO)->EnableWindow(TRUE);
}

void CTxMediaAppDlg::OnBnClickedBtnStopSendAudio()
{
	_txMedia->StopSendAudio(_audioChannel);
	GetDlgItem(IDC_BTN_START_SEND_AUDIO)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_STOP_SEND_AUDIO)->EnableWindow(FALSE);
}

void CTxMediaAppDlg::OnBnClickedBtnStartPlayout()
{
	_txMedia->StartPlayout(_audioChannel);
	GetDlgItem(IDC_BTN_START_PLAYOUT)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_STOP_PLAYOUT)->EnableWindow(TRUE);
}

void CTxMediaAppDlg::OnBnClickedBtnStopPlayout()
{
	_txMedia->StopPlayout(_audioChannel);
	GetDlgItem(IDC_BTN_START_PLAYOUT)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_STOP_PLAYOUT)->EnableWindow(FALSE);
}


void CTxMediaAppDlg::OnCbnSelchangeCboCamera()
{
	CComboBox* pBox = (CComboBox*)GetDlgItem(IDC_CBO_CAMERA);
	int item = pBox->GetCurSel();
	if (item >= 0)
	{
		CString szCamera;
		pBox->GetLBText(item, szCamera);
		std::string camera = szCamera.GetBuffer();
		_txMedia->StartPreviewLocalVideo(camera);
	}
}


void CTxMediaAppDlg::OnCbnSelchangeCboRecording()
{
	CComboBox* pBox = (CComboBox*)GetDlgItem(IDC_CBO_RECORDING);
	int item = pBox->GetCurSel();
	if (item >= 0)
	{
		CString szRecording;
		pBox->GetLBText(item, szRecording);
		std::string recording = szRecording.GetBuffer();
		_txMedia->SetRecordingDevice(recording);
	}
}


void CTxMediaAppDlg::OnCbnSelchangeCboPlayout()
{
	CComboBox* pBox = (CComboBox*)GetDlgItem(IDC_CBO_PLAYOUT);
	int item = pBox->GetCurSel();
	if (item >= 0)
	{
		CString szPlayout;
		pBox->GetLBText(item, szPlayout);
		std::string playout = szPlayout.GetBuffer();
		_txMedia->SetPlayoutDevice(playout);
	}
}


void CTxMediaAppDlg::OnBnClickedCheckAgc()
{
	CButton* button = (CButton*)GetDlgItem(IDC_CHECK_AGC);
	bool value = (bool)button->GetCheck();
	static int agc = 0;
	agc = agc++ % 5;
	_txMedia->SetAgcStatus(value, agc);
}


void CTxMediaAppDlg::OnBnClickedCheckEc()
{
	CButton* button = (CButton*)GetDlgItem(IDC_CHECK_EC);
	bool value = (bool)button->GetCheck();
	static int ec = 0;
	_txMedia->SetEcStatus(value, ec);
	if (value)
	{
		if (++ec >= 5)
		{
			ec = 0;
		}
	}
}

void CTxMediaAppDlg::OnBnClickedCheckNs()
{
	CButton* button = (CButton*)GetDlgItem(IDC_CHECK_NS);
	bool value = (bool)button->GetCheck();
	static int ns = 0;
	_txMedia->SetNsStatus(value, ns);
	if (value)
	{
		if (++ns >= 7)
		{
			ns = 0;
		}
	}
}

void CTxMediaAppDlg::OnBnClickedCheckAgcRemote()
{
	CButton* button = (CButton*)GetDlgItem(IDC_CHECK_AGC_REMOTE);
	bool value = (bool)button->GetCheck();
	static int agc = 0;
	_txMedia->SetRxAgcStatus(_audioChannel, value, agc);
	if (value)
	{
		if (++agc >= 5)
		{
			agc = 0;
		}
	}
}

void CTxMediaAppDlg::OnBnClickedCheckNsRemote()
{
	CButton* button = (CButton*)GetDlgItem(IDC_CHECK_NS_REMOTE);
	bool value = (bool)button->GetCheck();
	static int ns = 0;
	ns = ns++ % 7;
	_txMedia->SetRxNsStatus(_audioChannel, value, ns);
	if (value)
	{
		if (++ns >= 7)
		{
			ns = 0;
		}
	}
}

void CTxMediaAppDlg::OnBnClickedCheckMute()
{
	CButton* button = (CButton*)GetDlgItem(IDC_CHECK_MUTE);
	bool value = (bool)button->GetCheck();
	_txMedia->MuteInput(_audioChannel, value);
}

void CTxMediaAppDlg::OnBnClickedBtnRecordOutgoingVideo()
{
	if (_remoteVideoChannel < 0)
	{
		return;
	}

	static bool flag = false;
	if (!flag)
	{
		std::stringstream ss;
		ss << "d:/outgoing_" << GetTickCount() << ".avi";
		std::string fileName = ss.str();
		_txMedia->StartRecordOutgoingVideo(_remoteVideoChannel, fileName);
		((CButton*)GetDlgItem(IDC_BTN_RECORD_OUTGOING_VIDEO))->SetWindowText("停止录制本地视频");
	}
	else
	{
		_txMedia->StopRecordOutgoingVideo(_remoteVideoChannel);
		((CButton*)GetDlgItem(IDC_BTN_RECORD_OUTGOING_VIDEO))->SetWindowText("启动录制本地视频");
	}

	flag = !flag;

}

void CTxMediaAppDlg::OnBnClickedBtnRecordIncomingVideo()
{
	if (_remoteVideoChannel < 0)
	{
		return;
	}

	static bool flag = false;
	if (!flag)
	{
		std::stringstream ss;
		ss << "d:/incoming_" << GetTickCount() << ".avi";
		std::string fileName = ss.str();
		_txMedia->StartRecordIncomingVideo(_remoteVideoChannel, fileName);
		((CButton*)GetDlgItem(IDC_BTN_RECORD_INCOMING_VIDEO))->SetWindowText("停止录制远端视频");
	}
	else
	{
		_txMedia->StopRecordIncomingVideo(_remoteVideoChannel);
		((CButton*)GetDlgItem(IDC_BTN_RECORD_INCOMING_VIDEO))->SetWindowText("启动录制远端视频");
	}
	flag = !flag;
}
